package com.example.boot3.controller;


import com.alibaba.excel.EasyExcel;
import com.example.boot3.model.Use;
import com.example.boot3.model.Vehicle;
import com.example.boot3.service.UseService;
import com.example.boot3.util.JsonData;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;



@CrossOrigin(origins =  "*")
@RestController

public class UseController {
    @Autowired
    private UseService useService;

    @GetMapping("use_info")
    public JsonData getUseList(){
        return JsonData.buildSuccess(useService.getUseList());
    }


    @PostMapping("apply_vehicle")
    public  JsonData applyVehicle(@RequestBody Use use){
        Integer result = useService.applyVehicle(use);
        System.out.println(result);
        String msg="";
        if(result!=1){
            if(result == 2) {
                msg=use.getVehicle_id()+"出车中";
            } else if(result == 3) {
                msg=use.getVehicle_id()+"维修中";
            }
            return JsonData.buildError(msg);
        }

        return JsonData.buildSuccess(msg);

    }


    @GetMapping("fleet/use")
    public  JsonData getFleetUseList(){
        List<Map<String,String>> data = null;
        return JsonData.buildSuccess(useService.getFleetUseList());
    }


    /** bug&数据为空 */
    @GetMapping("use/export")
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=uses.xlsx");
        List<Use> data = useService.getUseList();
        /** 注意：使用EasyExcel导出Excel表格时，默认不支持 Date等非基本类型格式,统统改为改为String */
        EasyExcel.write(response.getOutputStream(), Use.class).sheet("使用信息").doWrite(data);
//        return JsonData.buildSuccess("");
    }

}
